function Middleware() {
    this.queue = []
}

Middleware.prototype.use = function (fn) {
    if (typeof fn !== 'function') {
        throw '中间件必须是一个函数'
    } else {
        this.queue.push(fn)
    }
    return this
}

Middleware.prototype.next = function() {
    if (this.task.length > 0) {
        var fn = this.task.shift()
        fn.call(this, this.next.bind(this))
    }
}

Middleware.prototype.go = function (cb) {
    this.task = this.queue.map(function(item) {
        return item
    })
    if (typeof cb === 'function') {
        this.task.push(cb)
    }
    this.next()
}

exports = module.exports = Middleware